<template>
    <div class="app-container">
      <el-form ref="queryForm" :model="queryParams" size="small" :inline="true">
       
        <el-form-item label="患者名称" prop="patientName">
          <el-input v-model="queryParams.patientName" placeholder="请输入内容" clearable style="width: 240px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
       
        <el-form-item label="创建时间" prop="signTime">
          <el-date-picker v-model="queryParams.signTime" value-format="yyyy-MM-dd" :picker-options="pickerOptions"
            type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" align="right">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"  class="add" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh"  class="delBtn" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" v-hasPermi="['bus:patient:add']" plain icon="el-icon-plus" class="add" @click="handleAdd">新增</el-button>
        </el-col>
        <right-toolbar :show-search.sync="showSearch" @queryTable="getListData" />
      </el-row> -->
      <el-table ref="table" :key="tableKey" v-loading="loading" :data="roleList" style="margin-top: 20px">
       
       
        <el-table-column label="患者名称" prop="patientName" />
        <el-table-column label="患者手机" prop="patientPhone" />
        <el-table-column label="患者身份证" prop="patientIdentity" />
        <el-table-column label="领取次数" prop="receiveTimes" />
        <el-table-column label="创建人" prop="createByName" />
        <el-table-column label="创建时间" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button v-hasPermi="['bus:patient:edit']" type="text" class="btnText" @click="handleUpdate(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getListData" />
      <el-dialog :title="title" :visible.sync="open" width="786px" append-to-body>
        <el-form ref="addForm" :model="addForm" :rules="addRules" label-position="top">
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="患者名称" prop="patientName">
                <el-input v-model="addForm.patientName" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="患者手机" prop="patientPhone">
                <el-input v-model="addForm.patientPhone" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item label="患者身份证" prop="patientIdentity">
                <el-input v-model="addForm.patientIdentity" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
            
          </el-row>
          
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" class="add" @click="submitForm">{{
        addForm.id ? "确认编辑" : "确认添加"
      }}</el-button>
        </div>
        <el-dialog :visible.sync="dialogVisible" :modal="false">
          <el-image :src="dialogImageUrl" :fit="fit"></el-image>
        </el-dialog>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import {
    getList,
    addData,
    editData,
    actionData,
  } from "@/api/medicalCharitableManage/index";
  import { getToken } from "@/utils/auth";
  import { getUserProfile } from "@/api/system/user";
  export default {
    name: "Role",
    dicts: ["sys_states"],
    data() {
      return {
        fit:'none',
        dialogVisible: false,
        dialogImageUrl: "",
        uploadUrl:
          process.env.VUE_APP_BASE_API + "/common/uploadPc/goodsRegister", // 上传的图片服务器地址
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        pickerOptions: {
          shortcuts: [
            {
              text: "最近一周",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit("pick", [start, end]);
              },
            },
            {
              text: "最近一个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit("pick", [start, end]);
              },
            },
            {
              text: "最近三个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit("pick", [start, end]);
              },
            },
          ],
        },
        user: {},
        fileList: [],
        title: "添加",
        open: false,
        tableKey: 0,
        // 遮罩层
        loading: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 10,
        // 角色表格数据
        roleList: [],
        // 查询参数
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          patientName: "",
          signTime: "",
        },
        addForm: {
          phone: "",
          signTime: "",
          description: "",
          imageUrls: [],
          remark: "",
          contactByName: "",
        },
        addRules: {
            patientPhone: [
            {
              required: true,
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur",
            },
          ],
          patientIdentity: [
            { required: true, message: "内容不能为空", trigger: "blur" },
          ],
          patientName: [
            { required: true, message: "内容不能为空", trigger: "blur" },
          ],
        },
        systemList: [],
      };
    },
    created() {
      this.getListData();
      getUserProfile().then((res) => {
        this.user = res.data;
      });
    },
    methods: {
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
        console.log(file.url);
      },
      // 上传前校检格式和大小
      handleBeforeUpload(file) {
        const type = ["image/jpeg", "image/jpg", "image/png", "image/svg"];
        const isJPG = type.includes(file.type);
        // 检验文件格式
        if (!isJPG) {
          this.$message.error(`图片格式错误!`);
          return false;
        }
        // 校检文件大小
        if (this.fileSize) {
          const isLt = file.size / 1024 < 500;
          if (!isLt) {
            this.$message.error(`上传文件大小不能超过 500 KB!`);
            return false;
          }
        }
        return true;
      },
      handleRemove(file, fileList) {
        console.log(file);
        const list = this.addForm.imageUrls.filter(
          (item) => item !== file.response.fileName
        );
        this.addForm.imageUrls = list;
      },
      handleExceed(files, fileList) {
        this.$message.warning(
          `当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length
          } 个文件`
        );
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`);
      },
      handleUploadSuccess(res, file) {
        // 如果上传成功
        if (res.code == 200) {
          this.addForm.imageUrls.push(res.fileName);
        } else {
          this.$message.error("图片上传失败");
        }
      },
      /** 查询列表 */
      getListData() {
        this.loading = true;
        getList({
          ...this.queryParams,
          startDate: this.queryParams.signTime[0],
          endDate: this.queryParams.signTime[1],
        }).then((response) => {
          this.roleList = response.data.records;
          this.total = response.data.total || 0;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.$nextTick(() => {
          this.getListData();
        });
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.pageNum = 1;
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 添加按钮操作 */
      handleAdd() {
        this.title = "添加";
        this.open = true;
        this.reset();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.title = "编辑";
        console.log(row);
        this.addForm =JSON.parse(JSON.stringify(row))
        // this.addForm.id = row.id;
        // this.addForm.phone = row.phone;
        // this.addForm.description = row.description;
        // this.addForm.remark = row.remark;
        // this.addForm.signTime = row.signTime;
        // this.addForm.imageUrls = row.imageUrls;
        // this.fileList = row.imageUrls.map((item) => {
        //   return {
        //     name: item.split("/").pop(),
        //     url: "/api" + item,
        //     // url: this.api + item,
        //   };
        // });
        this.open = true;
      },
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.addForm = {
            patientIdentity: "",
            patientName: "",
            patientPhone: ""
        };
        this.resetForm("addForm");
      },
      /** 按钮操作 */
      handleDelete(row) {
        actionData({ id: row.id, status: row.status == 0 ? 1 : 2 }).then(
          (response) => {
            this.$modal.msgSuccess("操作成功");
            this.getListData();
          }
        );
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["addForm"].validate((valid) => {
          if (valid) {
            
            if (this.addForm.id) {
              editData({
                ...this.addForm,
                contactByName: this.user.nickName,
              }).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getListData();
              });
            } else {
              addData({
                ...this.addForm,
                contactByName: this.user.nickName,
              }).then((response) => {
                this.$modal.msgSuccess("添加成功");
                this.open = false;
                this.getListData();
              });
            }
          }
        });
      },
    },
  };
  </script>
  <style lang="scss" scoped>
  .imgprev {
    width: 100vw;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
  
    img {
      max-width: 100%;
      max-height: 100%;
      display: block;
    }
  }
  
  .footerForm {
    margin-left: 200px;
  }
  
  .formBox {
    width: 600px;
    margin-top: 40px;
  }
  
  .btnText {
    color: #0052d9 !important;
  }
  
  .dialogForm {
    ::v-deep .el-form-item {
      margin-bottom: 10px;
    }
  
    ::v-deep.el-form--label-top .el-form-item__label {
      padding: 0;
    }
  }
  
  .add {
    background: #0052d9;
    color: #fff;
    border: none;
  }
  
  .delBtn {
    background: #e7e7e7;
    color: #000;
    border: none;
  }
  
  .open {
    width: 52px;
    height: 32px;
    background: #e3f9e9;
    border-radius: 3px 3px 3px 3px;
    border: 1px solid #2ba471;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 400;
    font-size: 14px;
    color: #2ba471;
  }
  
  .close {
    width: 52px;
    height: 32px;
    background: #d8d8d8;
    border-radius: 3px 3px 3px 3px;
    border: 1px solid #767676;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 400;
    font-size: 14px;
    color: #767676;
  }
  
  .statusBox {
    width: 100%;
    display: flex;
    justify-content: center;
  }
  
  .status_div {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 32px;
    background: #ffffff;
    border-radius: 3px;
    border: 1px solid #e7e7e7;
    font-size: 14px;
    background: #f3f3f3;
    color: rgba(0, 0, 0, 0.9);
    line-height: 1;
  
    &.jxz {
      background: #f2f3ff;
      border: 1px solid #0052d9;
      color: #0052d9;
    }
  
    &.ywc {
      background: #e3f9e9;
      border: 1px solid #2ba471;
      color: #2ba471;
    }
  }
  
  .img {
    width: 38px;
    height: 38px;
  }
  
  .img_box {
    display: flex;
    justify-content: center;
    gap: 10px;
  }
  </style>
  